
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : FSM
{
    public static PlayerController Instance { get; private set; }

    public override void Awake()
    {
        Instance = this;

        base.Awake();

        // 初始化输入控制
        parameter.inputSystem = new PlayerSystem();
        parameter.inputSystem.Player.Move.performed += Move;
        parameter.inputSystem.Player.Move.canceled += StopMove;
        parameter.inputSystem.Player.Dodge.started += Dodge;
        parameter.inputSystem.Player.MeleeAttack.started += MeleeAttack;

        SwitchActionMap(parameter.inputSystem.Player); // 切换到游戏操作的输入映射
    }

    void OnDisable()
    {
        // 禁用所有输入
        parameter.inputSystem.Disable();
    }

    public override void Update()
    {
        base.Update();
    }

    public void Move(InputAction.CallbackContext context)
    {
        parameter.inputDirection = parameter.inputSystem.Player.Move.ReadValue<Vector2>();
    }

    // 停止移动，将输入方向设为零向量
    public void StopMove(InputAction.CallbackContext context)
    {
        parameter.inputDirection = Vector2.zero;
    }

    // 触发闪避的方法
    public void Dodge(InputAction.CallbackContext context)
    {
        //如果当前不在冷却中，则开始闪避
        if (!parameter.isDodgeOnCooldown)
        {
            parameter.isDodging = true;
        }
    }

    // 近战攻击
    public void MeleeAttack(InputAction.CallbackContext context)
    {
        parameter.isMeleeAttack = true;
    }
}